package net.y2t72.microblog.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter {
	private String encoding = null;
	
	
	@Override
	/**
	 * 销毁
	 */
	public void destroy() {
		this.encoding = null;
	}
	
	@Override
	/**
	 * 过滤
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		//加载字符编码
		if (this.encoding != null && encoding.trim().length() != 0) {
			request.setCharacterEncoding(this.encoding);
			response.setCharacterEncoding(this.encoding);
		}
		
		//将处理权给下一个处理器
		chain.doFilter(request, response);
	}

	@Override
	/**
	 * 初始化
	 */
	public void init(FilterConfig config) throws ServletException {
		this.encoding = config.getInitParameter("encoding");
	}
}
